package com.zdxlz.mongodbDemo.service;

import com.zdxlz.mongodbDemo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    private final UserRepository repository;

    @Autowired
    public UserService(UserRepository repository) {
        this.repository = repository;
    }

    public User findById(String id) {
        return repository.findById(id).get();
    }

    public Page<User> findPage(Integer pageSize, Integer pageNum) {
        return repository.findAll(PageRequest.of(pageNum, pageSize));
    }

    public User save(User user) {
        return repository.save(user);
    }

    public User update(User user) {
        return repository.save(user);
    }

    public void deleteById(String id) {
        repository.deleteById(id);
    }
}
